function SortTable(sTableId,sTBodyId,step)
{
	if(step==null ||step=="")
		step=1;
	if(step>2)
	  step=2;
	this.step= step;
	this.tableId = sTableId;
	this.tBodyId = sTBodyId;
	this.rowCount = 0;// 记录数
	this.__oTable__ = null;// 表格引用
	this.__oRow__ = null;  // sort
	this.__oTBody__ = null;// 要分页内容
	this.__dataRows__ = 0;// 记录行引用
	this.__oldTBody__ = null;
	this.__init__(); // 初始化;
	
};

SortTable.prototype.__init__ = function()
{
	this.__oTable__ = document.getElementById(this.tableId);// 获取table引用
	this.__oRow__ = this.__oTable__.rows[0];
	this.__oTBody__ = this.__oTable__.tBodies[this.tBodyId];// 获取tBody引用
	this.__dataRows__ = this.__oTBody__.rows;
	this.rowCount = this.__dataRows__.length;
	for (var i = 0; i < this.__oRow__.cells.length; i++)
	{
		var cell = this.__oRow__.cells[i];
		var txt = this.__getText__(cell);
		cell.innerHTML = 
			'<a href="#" style="cursor:pointer" class="sortheader"  onclick="sortTable.sort(this,\'desc\');return false;">' + txt+'</a>';
	};
};

SortTable.prototype.__getText__ = function(el)
{
	 if (typeof el == "string") return el;
	 if (typeof el == "undefined") { return el; };
	 if (el.innerText) return el.innerText; // Not needed but it is faster
	 var str = "";
	 
	 var cs = el.childNodes;
	 var l = cs.length;
	 for (var i = 0; i < l; i++) 
	 {
	  switch (cs[i].nodeType) 
	  {
	   case 1: // ELEMENT_NODE
	    str += this.__getText__(cs[i]);
	    break;
	   case 3: // TEXT_NODE
	    str += cs[i].nodeValue;
	    break;
	  }
	 }
	 return str;
}

SortTable.prototype.__cloneRows__ = function(){
	var tempRows = [];
	for(var i=0; i<this.__dataRows__.length; i++){
	/*
	code:this.dataRows[i].cloneNode(param), 
	param = 1 or true:复制以指定节点发展出去的所有节点,
	param = 0 or false:只有指定的节点和它的属性被复制.
	*/
	tempRows[i] = this.__dataRows__[i].cloneNode(1);
	}
	return tempRows;
};

SortTable.prototype.sort = function(td, order)
{
	if (this.__oTBody__ == null)
		return;
	
	var column = td.parentNode.cellIndex;
	var tdtext = this.__getText__(td.parentNode);

	var tempRows = this.__cloneRows__();
	
    for(a=0;a<=tempRows.length-1;a+=this.step)
    {
    	for(b=a+this.step;b<=tempRows.length-1;b+=this.step)
    	{
    		var value1=this.__getText__(tempRows[a].cells[column]);
    		var value2=this.__getText__(tempRows[b].cells[column]);
    		if(value1)
    		{
    		if (value1.match(/^[\d\.]+$/))
	    		{
	    		    if(order=="asc")
	    		    {
			    		if(value1*1>value2*1)
			    		{
			    			var row1 = tempRows[a];
			    			tempRows[a] = tempRows[b];
			    			tempRows[b] = row1;
			    			if(this.step>1)
			    			{
				    			var row2 = tempRows[a+1];
				    			tempRows[a+1] = tempRows[b+1];
				    			tempRows[b+1] = row2;
			    			}
			    		}
	    		    }
	    		    else
	    		    {
	    		    	if(value1*1<value2*1)
			    		{
			    			var row1 = tempRows[a];
			    			tempRows[a] = tempRows[b];
			    			tempRows[b] = row1;
			    			if(this.step>1)
			    			{
				    			var row2 = tempRows[a+1];
				    			tempRows[a+1] = tempRows[b+1];
				    			tempRows[b+1] = row2;
			    			}
			    		}
	    		    }
	    		}
	    		else
	    		{
	    			 if(order=="asc")
		    		 {
		    			if(value2<value1)
			    		{
			    			var row1 = tempRows[a];
			    			tempRows[a] = tempRows[b];
			    			tempRows[b] = row1;
			    			if(this.step>1)
			    			{
				    			var row2 = tempRows[a+1];
				    			tempRows[a+1] = tempRows[b+1];
				    			tempRows[b+1] = row2;
			    			}
			    		}
		    		 }else
	    		    {
		    			 if(value2>value1)
				    		{
				    			var row1 = tempRows[a];
				    			tempRows[a] = tempRows[b];
				    			tempRows[b] = row1;
				    			if(this.step>1)
				    			{
					    			var row2 = tempRows[a+1];
					    			tempRows[a+1] = tempRows[b+1];
					    			tempRows[b+1] = row2;
					    		}
				    		}
	    		    }
	    		}
    		}
    	}
    }
    
    var removedTBody = this.__oTable__.removeChild(this.__oTBody__);
    var newTBody = document.createElement("TBODY");
    newTBody.setAttribute("id", this.tBodyId);

    for(var i=0; i < this.rowCount; i++){
    	newTBody.appendChild(tempRows[i]);
    }
    this.__oTable__.appendChild(newTBody);

    this.__dataRows__ = tempRows;
    this.__oTBody__ = newTBody;
    
    tdtext  = tdtext.replace("↑","").replace("↓","");
    if(order=="asc")
    {
    	order="desc";
    	tdtext = tdtext+"↓";
    }
    else
    {
    	order = "asc";
    	tdtext = tdtext+"↑";
    }
    td.parentNode.innerHTML="<a href=\"#\" class=\"sortheader\" style=\"cursor:pointer\" onclick='sortTable.sort(this,\""+order+"\");return false;'>"+tdtext+"</a>";
};

function getParent(el, pTagName) {
	 if (el == null) return null;
	 else if (el.nodeType == 1 && el.tagName.toLowerCase() == pTagName.toLowerCase()) // Gecko
																						// bug,
																						// supposed
																						// to
																						// be
																						// uppercase
	  return el;
	 else
	  return getParent(el.parentNode, pTagName);
	}
